﻿///<reference path="/Scripts/ClientComponents/jquery/jquery-1.7.2-vsdoc.js" />
///<reference path="/Scripts/InnovacallJS/_Layout/_Layout.js" />
///<reference path="/ScriptsG/inoAdvancedSamples_AddressesManagementDynamic.js" />
//INNOVACALL FRAMEWORK MUST BE IN ALL VIEW JAVASCRIPT
var selectedTypeAddressCount = 0;
$.fn.DocReady = function () {
    $.fn.PrepareLayoutMenus();
    $.fn.TabifyCodeFiles();
    //Add call to execute when document is ready
    $("#cmdOk").live("click", $.fn.cmdOkClick);
    $("#cmdDelete").live("click", $.fn.cmdDeleteClick);
    $.fn.PrepareForm(-1);
    if (scriptToExecute != null) {
        if (scriptToExecute != "") {
            eval(scriptToExecute);
            return false;
        }
    }
}
$.fn.PrepareForm = function (addressId) {
    $.fn.MakeListSelectable();
    $("#lstSelector").find(".ui-selectee[value=" + addressId + "]").addClass("ui-selected");
    $("#CurrentAddress_Country").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/api/country/CountryNameStartWith",
                data: '"' + request.term + '"',
                success: function (data) {
                    response(data);
                }
            });
        }
    });
    if (addressId >= 1) {
        $("#cmdDelete").removeAttr("disabled");
    } else {
        $("#cmdDelete").attr("disabled", "disabled");
    }
    $("#CurrentAddress_Id").val("-1");
}
$.fn.MakeListSelectable = function (){
    $("#lstSelector").selectable(
    {
        stop: function (e, ui) {
            $(".ui-selected:first", this).each(function () {
                $(this).siblings().removeClass("ui-selected");
            });
        },
        selected: $.fn.AddressSelected
    });    
}
$.fn.AddressSelected = function (e, ui) {
    var v = $(ui.selected).attr("value")
    if (v < 1) {//Strange bug fix v must be -1 but I get 0 ?
        v = -1;
        $("#cmdDelete").attr("disabled", "disabled");
        //Empty form
        $(".divAddressForm").find("input[type=text].k-input").each(function () {
            $(this).attr("value", "");
        });
        $(".divAddressForm").find("input[type=checkBox]").each(function () {
            $(this).removeAttr("checked");
        });
        $("#CurrentAddress_Id").val("-1");
    } else {
        var url = "/AdvancedSamples_AddressesManagement/ContactAddressGet/" + v;
        $("#cmdDelete").removeAttr("disabled");
        $.ajax(
	    {
	        type: "POST",
	        url: url,
	        data: null,
	        async: false,
	        success: function (r) {
	            if (r.success) {
	                $(".divAddressForm").html(r.htmlContent);
	            } else {
	                $.fn.jqDialogAlert(ErrMsgAddressRetrieval_Title, r.message, null);
	            }
	        },
	        error: function (xhr, ajaxOptions, thrownError) {
	            $.fn.jqDialogAlert(ErrMsgAddressRetrieval_Title, xhr.responseText, null);
	        }
	    });
    }
}
$.fn.cmdOkClick = function (e, ui) {
    //AddressTypeValidation
    selectedTypeAddressCount = 0;
    $(".divAddressForm").find("input[type=checkbox]").each(function () {
        if ($(this).attr("checked") == "checked") {
            selectedTypeAddressCount++;
        }
    });
    if (selectedTypeAddressCount == 0) {
        $.fn.jqDialogAlert(ErrMsgAtLeastOneAddressTypeMustBeSelected_Title, ErrMsgAtLeastOneAddressTypeMustBeSelected_Message, null);
        $(".divAddressForm").find("input[type=checkbox]").each(function () {
            $(this).addClass("input-validation-error");
        });
        e.preventDefault();
        return false;
    }
    var v = $("#lstSelector").find(".ui-selected").attr("value");
    if (v < 1) v = -1;
    $("#form0").attr("action", "/AdvancedSamples_AddressesManagement/AddUpdate/" + v);
}

$.fn.cmdDeleteClick = function (e, ui) {
    e.preventDefault();
    $.fn.jqDialogConfirm(DeleteDialogConfirmTitle, DeleteDialogConfirmMessage, DeleteDialogConfirmConfirmButtonTitle, DeleteDialogCancelConfirmButtonTitle, $.fn.DeleteAddress);
    return false;
}
$.fn.DeleteAddress = function (e, ui) {
    var v = $("#lstSelector").find(".ui-selected").attr("value");
    $("#form0").attr("action", "/AdvancedSamples_AddressesManagement/Delete/" + v);
    $("#form0").submit();
    $(this).dialog('close');
}

$.fn.MainAjaxSuccess = function (a, b, c) {
    if (a.success) {
        $(a.divToUpdate).html(a.htmlContent);
        if (a.scriptToExecute != null) {
            eval(a.scriptToExecute);
        }
    }
}

